<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>工作派遣单</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<style type="text/css">
		 .left-label {
		 	width: 120px !important
		 }
	</style>
	<link rel="stylesheet" href="${pageContext.request.contextPath }/assets/css/layui.css"  media="all">
	<link rel="stylesheet" href="${pageContext.request.contextPath }/assets/css/toastr.min.css"  media="all">
</head>
<body>
<div class="demoTable" style="padding:10px 0 0 15px">
	<label class="layui-form-label">委派人员</label>
    <div class="layui-inline">
    	<input class="layui-input" name="searchPeople" id="searchPeople" placeholder="" style="display: inline; width: 80%">
    </div>
    <button class="layui-btn layui-btn-blue" style="margin: 0 0 0 10px" data-type="reload"><i class="layui-icon layui-icon-search">  搜索</i></button>
</div>

<table class="layui-hide" id="tableList" lay-filter="tableList"></table>

<div id="addDivID" hidden="hidden">
	<form class="layui-form" id="addFormID" action="">
	   <input type="hidden" id="id" name="id">
	   <div class="layui-form-item">
	    <label class="layui-form-label" style="font-size: 24px; width:200px;">工作派遣单</label>
	    <label class="layui-form-label">状态</label>
	    <div class="layui-input-block">
	      <input type="radio" name="state" value="紧急" title="紧急">
	      <input type="radio" name="state" value="一般" title="一般">
	    </div>
	  </div>
	  
	   <div class="layui-form-item">
	   	<div class="layui-inline">
		    <label class="layui-form-label">部门</label>
		    <div class="layui-input-inline">
		      <select name="old" id="old"  lay-verify="old"  class="layui-input"  lay-filter="old">
		      </select>
		    </div>
		    <div class="layui-inline">
				<font style="color:red; font-size: 24px;">*</font>
	      	</div>
		 </div>
		 <div class="layui-inline">
		    <label class="layui-form-label">派往部门</label>
		    <div class="layui-input-inline">
		      <select name="news" id="news" lay-verify="news">
		      </select>
		    </div>
		    <div class="layui-inline">
				<font style="color:red; font-size: 24px;">*</font>
	      	</div>
		 </div>
		</div>	
		
		<div class="layui-form-item">
	    
	    <div class="layui-inline">
	     	<label class="layui-form-label">部门领导</label>
		    <div class="layui-input-inline">
		    	<input class="layui-input" type="text" id="leader" name="leader" lay-verify="leader">
		    </div>
		    <div class="layui-inline">
				<font style="color:red; font-size: 24px;">*</font>
		      </div>
		</div>
		
		 <div class="layui-inline">
	      <label class="layui-form-label">委派人员</label>
	      <div class="layui-input-inline">
	        <select name="people" lay-search="" id="people" lay-verify="people">
	        	<option value="">直接选择或搜索选择</option>
	        </select>
	      </div>
	      <div class="layui-inline">
				<font style="color:red; font-size: 24px;">*</font>
		      </div>
	    </div>
	  </div>
	  
	  <div class="layui-form-item">
	  
	    <div class="layui-inline">
	      <label class="layui-form-label">开始时间</label>
	      <div class="layui-input-inline">
	        <input type="text" name="starttime" id="starttime" lay-verify="starttime" autocomplete="off" class="layui-input">
	      </div>
	    </div>
	    <div class="layui-inline">
	      <label class="layui-form-label">结束时间</label>
	      <div class="layui-input-inline">
	        <input type="text" name="endtime" id="endtime" lay-verify="endtime" autocomplete="off" class="layui-input">
	      </div>
	    </div>
	    
	    <div class="layui-inline">
		    <label class="layui-form-label">合计工时</label>
		    <div class="layui-input-inline">
		    	<input class="layui-input" type="text" id="longs" name="longs" onfocus="diff()">
		    </div>
		 </div>
	  </div>
	  
	   <div class="layui-form-item layui-form-text">
	    <label class="layui-form-label">工作内容</label>
	    <div class="layui-input-block" style="width: 84%">
	      <textarea placeholder="请输入内容" class="layui-textarea" name="content" id="content"></textarea>
	    </div>
	  </div>
	  
	   <div class="layui-form-item">
	    <label class="layui-form-label">备注项目号</label>
	    <div class="layui-input-block" style="width: 84%">
	      <input type="text" name="project" id="project" lay-verify="project" autocomplete="off" class="layui-input">
	    </div>
	  </div>
	  
		<button class="layui-btn layui-btn-blue" lay-submit lay-filter="addForm" id="save" style="margin-left:160px; margin-bottom: 20px">提交</button>&emsp;&emsp;&emsp;&emsp;<button type="reset" id="reset" class="layui-btn layui-btn-primary" style="margin-bottom: 20px">重置</button>
	</form>
</div>
<script type="text/html" id="toolbar">
	<div class="layui-btn-container">
		<button class="layui-btn layui-btn-blue" lay-event="add"><i class="layui-icon layui-icon-add-1">  新增</i></button>
		<button class="layui-btn layui-btn-blue" lay-event="delete"><i class="layui-icon layui-icon-delete">批量删除</i></button>
	</div>
</script>
 
<script type="text/html" id="rowToolbar">
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="confirm">确认</a>
	<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="${pageContext.request.contextPath }/assets/layui.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/assets/layui.all.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/assets/js/jquery.min.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/assets/js/toastr.min.js" charset="utf-8"></script>

<script type="text/javascript">

function diff() {
	var start = $("#starttime").val();
	var end = $("#endtime").val();
	timeFn(start,end);
}

function timeFn(startTime,endTime) {//di作为一个变量传进来
	
	var start = new Date(startTime);
	var end = new Date(endTime);
    var dateDiff = end - start;//时间差的毫秒数
    var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000));//计算出相差天数
    
    var leave1=dateDiff%(24*3600*1000)    //计算天数后剩余的毫秒数
    var hours=Math.floor(leave1/(3600*1000))//计算出小时数
    //计算相差分钟数
    var leave2=leave1%(3600*1000)    //计算小时数后剩余的毫秒数
    var minutes=Math.floor(leave2/(60*1000))//计算相差分钟数
    //计算相差秒数
    var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
    var seconds=Math.round(leave3/1000)
   // alert(" 相差 "+dayDiff+"天 "+hours+"小时 "+minutes+" 分钟"+seconds+" 秒")
    
    $("#longs").val(dayDiff+"天 "+hours+"小时 "+minutes+" 分钟"+seconds+" 秒");
    
    return;
}
	
</script>

<script type="text/javascript">

	layui.use(['table','layer','upload','form','laydate'], function(){
		var table = layui.table;
		var layer = layui.layer;
		var laydate = layui.laydate;
	    var form = layui.form;
	    
		var $ = layui.jquery, active = {
			reload:function () {
				var searchPeople = $("#searchPeople").val();
				table.reload('contenttable',{
					method:'get',
					where:{"people":searchPeople}
				});
			}
		}
		
		form.on('select(old)', function(data){
			var old= data.value
			if(old != null && old != "" && old !=undefined ) {
				$.ajax({
			        type: "post",
			        url: "${pageContext.request.contextPath }/dispatch/queryDepartmentLeder.do",
			        data:{"name":old},
			        async: false,
			        success: function (data) {
			        	$("#leader").val(data);
			        }                                 
			  	});
			}else {
				$("#leader").val("");
			}
		});  
		
		$.ajax({
	        type: "post",
	        url: "${pageContext.request.contextPath }/dispatch/queryUser.do",
	        dataType: 'JSON',
	        async: false,
	        success: function (datas) {
	              //设置下拉列表中的值的属性
	              for ( var i = 0; i < datas.length; i++) {
                 	 //增加下拉列表。
                  	$("#people").append("<option value='"+datas[i].userName+"'>"+datas[i].userName+"</option>");
				}
	        }
	  	});
		
		 $.ajax({
	        type: "post",
	        url: "${pageContext.request.contextPath }/dispatch/queryDepartment.do",
	        dataType: 'JSON',
	        async: false,
	        success: function (data) {
	              //设置下拉列表中的值的属性
	       		 $("#old").append('<option value="">请选择</option>'); 
	       		 $("#news").append('<option value="">请选择</option>'); 
	              for ( var i = 0; i < data.length; i++) {
                 	 //增加下拉列表。
                  	$("#old").append("<option value='"+data[i].name+"'>"+data[i].name+"</option>");
                  	$("#news").append("<option value='"+data[i].name+"'>"+data[i].name+"</option>");
				}
	        }
	  	});
		 
		form.render();
		
		$('.layui-btn').on('click', function(){
	        var type = $(this).data('type');
	        active[type] ? active[type].call(this) : '';
	    });
		
		table.render({
		    elem: '#tableList'
		    ,url:'${pageContext.request.contextPath }/dispatch/selectList.do'
		    ,toolbar: '#toolbar'
		    ,title: '工作派遣单'
		    ,id :'contenttable'
		    ,limits:[10,20,30]
		    ,cols: [[
				      {type: 'checkbox', fixed: 'left'},
				      {field:'id', title:'ID'},
				      {field:'state', title:'状态',width:60},
				      {field:'old', title:'部门',width:120},
				      {field:'news', title:'派往部门',width:120},
				      {field:'longs', title:'合计工时',width:100},
				      {field:'people', title:'委派人员',width:100},
				      {field:'starttime', title:'开始时间',width:160},
				      {field:'endtime', title:'完成时间',width:160},
				      {field:'content', title:'工作内容',width:160},
				      {field:'project', title:'备注项目号',width:110},
				      {field:'datetime', title:'时间',width:110},
				      {field:'leader', title:'部门领导',width:100},
				      {field:'confirm', title:'用工部门确认',width:130},
				      {field:'remark', title:'确认人',width:100},
				      {field:'remark2', title:'确认时间',width:130},
				      {fixed:'right', title:'操作', toolbar: '#rowToolbar', width:160}
				    ]],
		    page: true,
		    done: function () {
		        $("[data-field='id']").css('display','none');
		    }
		});
		
		
	//头工具栏事件
	table.on('toolbar(tableList)', function(obj){
		var checkStatus = table.checkStatus(obj.config.id);
	    switch(obj.event){
	    	case 'add':
	    		layer.open({
					type: 1 					//Page层类型
			  		,area: ['1000px', '600px'] 	//宽  高
			  		,title: '新增'
			  		,shade: 0.6 				//遮罩透明度
			  		,maxmin: true 				//允许全屏最小化
			  		,anim: 1 					//0-6的动画形式，-1不开启
			  		,content: $("#addDivID")
			  		,end: function(){
			  			var formDiv = document.getElementById('addDivID');
					  	formDiv.style.display = '';
			  			$("#addDivID").hide();
			  		} 
	    		
		    		,success: function(layero, index){
		    			
		    			
		    			$("#reset").click();
		    			layui.form.render();
		    		}
				});
	      	break;
	    	case 'delete':
	        	var data = checkStatus.data;
	        	var idArr = new Array();
	        	if(data.length==0){
	        		toastr.warning("请至少选择一条记录！");
	        	}else {
	        		var ids = "";
	        		for(var i=0;i<data.length;i++){
		   			 if(data[i].confirm == '已确认') {
		   				 toastr.warning("已确认，不能删除！");
		   				 return;
		   			 }	
	        		}
	        		
		        	for(var i=0;i<data.length;i++){
		        		 if(i != data.length-1) {
			        	 	ids +=data[i].id+",";
			        	 }else {
			        	 	ids +=data[i].id;
			        	 }
		        	}
		        	layer.confirm('确定删除吗？', function(index){
				        $.ajax({
					    	type:'post',
					    	url:'${pageContext.request.contextPath }/dispatch/delete.do',
					    	data:{"ids":ids},
					    	success:function(data){
					    		if(data > 0){
					    			toastr.success("删除成功！");
					    			setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
										window.location.reload();  
									},2000);
					    		}else{
					    			toastr.warning("删除失败！");
					    		}
					    	}
					    })
		        	})
	        	}
	      	break;
	      	case 'select':
	      		console.log(obj)
	      	break;
	    };
	});
  
	//监听行工具事件
	table.on('tool(tableList)', function(obj){
		var data = obj.data;
		if(obj.event === 'del'){
			var confirm = obj.data.confirm;
			 if(confirm == '已确认') {
				 toastr.warning("已确认，不能删除！");
				 return;
			 }	
		  layer.confirm('确定删除吗？', function(index){
		    var id = obj.data.id;
		    $.ajax({
		    	type:'post',
		    	url:'${pageContext.request.contextPath }/dispatch/delete.do',
		    	data:{"ids":id},
		    	success:function(data){
		    		if(data>0){
		    			toastr.success("删除成功！");
		    			setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
							// 父页面刷新
							window.location.reload();  
						},2000);
		    		}else{
		    			toastr.warning("删除失败！");
		    		}
		    	}
		    		
		    })
		    layer.close(index);
		  });
		}else if(obj.event === 'edit'){
			 var confirm = obj.data.confirm;
			 if(confirm == '已确认') {
				 toastr.warning("已确认，不能修改！");
				 return;
			 }	
			
			layer.open({
				  type: 1 		//Page层类型
				  ,area: ['1000px', '600px'] //宽  高
				  ,title: '编辑'
				  ,shade: 0.6 	//遮罩透明度
				  ,maxmin: true //允许全屏最小化
				  ,anim: 1 		//0-6的动画形式，-1不开启
				  ,content:$("#addDivID")
				  ,success: function () {
					//回显表单数据
					for(var i=0;i<Object.entries(data).length;i++) {
						var id = '#' + Object.entries(data)[i][0];
						var text = Object.entries(data)[i][1];
						$(id).val(text);
						
						 $("input[name=state][value= '紧急']").attr("checked", data.state == "紧急" ? true : false);
		                 $("input[name=state][value= '一般']").attr("checked", data.state == "一般" ? true : false);
					}
					
					$.ajax({
				        type: "post",
				        url: "${pageContext.request.contextPath }/dispatch/queryUser.do",
				        dataType: 'JSON',
				        async: false,
				        success: function (datas) {
				              //设置下拉列表中的值的属性
				              for ( var i = 0; i < datas.length; i++) {
			                 	 //增加下拉列表。
			                  	$("#people").append("<option value='"+datas[i].userName+"'>"+datas[i].userName+"</option>");
			                  	if(datas[i].userName == data.people){
			                        $("#people").val(data.people);
			                    }
							}
				        }
				  	});
					 $.ajax({
					        type: "post",
					        url: "${pageContext.request.contextPath }/dispatch/queryDepartment.do",
					        dataType: 'JSON',
					        async: false,
					        success: function (data) {
					              //设置下拉列表中的值的属性
					       		 $("#old").append('<option value="">请选择</option>'); 
					       		 $("#news").append('<option value="">请选择</option>'); 
					              for ( var i = 0; i < data.length; i++) {
				                 	 //增加下拉列表。
				                  	$("#old").append("<option value='"+data[i].name+"'>"+data[i].name+"</option>");
				                  	$("#news").append("<option value='"+data[i].name+"'>"+data[i].name+"</option>");
				                  	if(data[i].name == data.old){
				                        $("#old").val(data.old);
				                    }
				                	if(data[i].name == data.news){
				                        $("#news").val(data.news);
				                    }
								}
					        }
					  	});
					
					form.render();
				  }	
				  ,end: function(){
			  			$("#addDivID").hide();
			  		} 
			});
		}else if(obj.event === 'confirm'){
			var data = obj.data;
			 var id = obj.data.id;
			 var confirm = obj.data.confirm;
			 if(confirm == '已确认') {
				 toastr.warning("已确认，请勿重复操作！");
				 return;
			 }	
			 
			    $.ajax({
			    	type:'post',
			    	url:'${pageContext.request.contextPath }/dispatch/confirm.do',
			    	data:{"id":id},
			    	success:function(data){
			    		if(data>0){
			    			toastr.success("已确认！");
			    			setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
								// 父页面刷新
								window.location.reload();  
							},2000);
			    		}else{
			    			toastr.warning("确认失败！");
			    		}
			    	}
			    		
			    })
		}
	});
	
	/**
     * 表单校验
     */
    form.verify({
    	old: function(value){
    		if(value == ''){
    			return '部门不能为空';
    		}
    	},
    	news: function(value){
    		if(value == ''){
    			return '派往部门不能为空';
    		}
    	},
    	leader: function(value){
    		if(value == ''){
    			return '部门领导为空';
    		}
    	},
    	people: function(value){
    		if(value == ''){
    			return '委派人员不能为空';
    		}
    	},
    });
    /**
     * 通用表单提交(AJAX方式)（新增）
     */
    form.on('submit(addForm)', function (data) {
      $("#save").attr("disabled",true);
      var datas=data.field;
   	  var action=data.form.action;
   	  var id = $("#id").val();
   	  if(id == "" || id == undefined || id == null) {
   		$.ajax({
   			  url : '${pageContext.request.contextPath}/dispatch/create.do',
     		  data:datas,
     		  type:"POST",
     		  dataType:"json",
     		  success:function(result){
     			 if(result > 0) {
     				toastr.success("新增成功！");
     				$("#reset").click();
     				setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
  					window.location.reload();  
  				},2000);
     			 }else {
     				 toastr.warning("新增失败！");
     				 return false;
     			 }
     		  },
     	  });
   	  }else {
   		$.ajax({
			url : '${pageContext.request.contextPath}/dispatch/edit.do',
			 data:datas,
    		  type:"POST",
    		  dataType:"json",
    		  success:function(result){
    			 if(result > 0) {
    				toastr.success("修改成功！");
    				$("#reset").click();
    				setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
 					window.location.reload();  
 				},2000);
    			 }else {
    				 toastr.warning("修改失败！");
    				 return false;
    			 }
    		  },
   		});
   	  }
   	  return false;
    });
    
    
    /**
     * 通用表单提交(AJAX方式)
     */
   /*  form.on('submit(subform)', function (data) {
    	
			return false;
    }); */
    
    
  //时间检索
    laydate.render({
      elem: '#starttime',
      type: 'datetime',
      format: 'yyyy-MM-dd HH:mm:ss'
    });
    laydate.render({
      elem: '#endtime',
      type: 'datetime',
      format: 'yyyy-MM-dd HH:mm:ss'
    });
});

toastr.options.positionClass = 'toast-top-center'; //提示框的位置设置为中上
</script>

</body>
</html>